home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 4796 < prev    next >
Encoding:
Text File  |  1996-08-06  |  2.3 KB  |  70 lines

  1. Path: gate.itron.com!usenet
  2. From: ronald.ten-hove@itron.com (Ron Ten-Hove)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Two way communication between objects
  5. Date: 31 Jan 1996 20:44:41 GMT
  6. Organization: Itron Inc.
  7. Message-ID: <4eokbp$1gk@gate.itron.com>
  8. References: <310ACCE2.2600@werple.mira.net.au>
  9. Reply-To: ronald.ten-hove@itron.com (Ron Ten-Hove)
  10. NNTP-Posting-Host: itron12-23.itron.com
  11. X-Newsreader: IBM NewsReader/2 v1.02
  12.  
  13. In <310ACCE2.2600@werple.mira.net.au>, Ross Forder <erosco@werple.mira.net.au> writes:
  14. >I have to apologise what what is probably a stupid question but I'm only
  15. >a beginner at some of this stuff.
  16. >
  17. >I am trying to write a VERY simple client and server set of objects. I 
  18. >would like to client to register with the server at ctor time and have 
  19. >the server know about the client by saving a pointer to the client 
  20. >so he can callback to a method called say 'event'. 
  21.  
  22. I assume that you are using the terms ``client'' and ``server'' in 
  23. a generic sense, and you are *not* trying to implement a client/
  24. server system across a communications network or using some 
  25. other IPC mechanism.
  26.  
  27. >The problem is the fact that they will both need a pointer to each other 
  28. >and this seems impossible using strong typing. Is there a simple 'object 
  29. >oriented' way to do this?
  30.  
  31. myFile.h
  32.  class Client;    // forward
  33.  class Server { 
  34.  public:
  35.    virtual void EventHandler( int eventType, Client & theClient );
  36.    enum { RegisterClient, DeregisterClient, LastEvent };
  37.  };
  38.  
  39.  class Client {
  40.  protected:
  41.    Server & itsServer;
  42.  public:
  43.    Client( Server & theServer ) : itsServer( theServer )
  44.      {
  45.     itsServer.EventHandler( Server :: RegisterClient, *this );
  46.     }
  47.    virtual ~Client()
  48.     {
  49.     itsServer.EventHandler( Server :: DeregisterClient, *this );
  50.     }
  51.    virtual void EventHandler( int event, Server & theServer );
  52.  };
  53.  
  54. >I have been able to make this work by having the server only know about 
  55. >a parent class of the client (say eventhandler) but this is still not a 
  56. >bulletproof solution.
  57.  
  58. Two things to keep in mind:
  59.  
  60.     1.  Separate source & header files for *declaring* and
  61.         *implementing* classes.
  62.     2.  Use polymorphism. The server doesn't need to know
  63.         much about clients, and vice versa, as long as they
  64.         conform to a protocol.  In the example above, the
  65.         protocol is implemented using the virtual function
  66.         EventHandler in both Client and Server.
  67.  
  68. Good luck!
  69. -Ron
  70.